package com.aboo.controller;

import com.aboo.domain.SysUser;
import com.aboo.frame.TokenUtil;
import com.aboo.service.SysUserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Resource
    private SysUserService userService;

    @PostMapping(value = "/login")
    @ResponseBody
    public Map<String, Object> login(String username, String password) {
        Map<String, Object> map = new HashMap<>();
        SysUser user = new SysUser(username, password);

        if (userService.login(user)) {
            String token = TokenUtil.sign(user);
            if (token != null) {
                map.put("code", "10000");
                map.put("message", "认证成功");
                map.put("token", token);
                return map;
            }
        }

        map.put("code", "0000");
        map.put("message", "认证失败");
        return map;
    }

    @PostMapping(value = "/getList")
    public List<SysUser> getList() {
        List<SysUser> userList = userService.getList();
        return userList;
    }

}